﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;

namespace KY.Converter
{
    public static class DataTableToJson
    {
        public static string DataTableToJsonWithJsonNet(this DataTable table)
        {
            string JsonString = string.Empty;
            JsonString = Newtonsoft.Json.JsonConvert.SerializeObject(table);
            return JsonString;
        }

        /// <summary>
        /// 主要用于将界面传过来的值保存为操作数据库的存储模型
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static Dictionary<string, object> DictionaryStringToObject(this Dictionary<string, string> keyValues)
        {
            Dictionary<string, object> keys = new Dictionary<string, object>();
            foreach (var item in keyValues)
            {
                keys.Add(item.Key, item.Value);
            }
            return keys;
        }

        /// <summary>
        /// 主要用于将界面传过来的值保存为操作数据库的存储模型
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static Dictionary<string, object> DictionaryToObject(this Dictionary<string, StringValues> keyValues)
        {
            Dictionary<string, object> keys = new Dictionary<string, object>();
            foreach (var item in keyValues)
            {
                if (item.Value.Count == 0)
                {
                    continue;
                }
                else if (item.Value.Count == 1 &&string.IsNullOrEmpty( item.Value[0]))
                {
                    continue;
                }
                else if (item.Value.Count==1&&!string.IsNullOrEmpty(item.Value[0]))
                {
                    keys.Add(item.Key, item.Value[0]);
                }
                else {
                    keys.Add(item.Key, item.Value.ToArray());
                }
                
            }
            return keys;
        }

        public static Dictionary<string, object> Convert(this Dictionary<string, object[]> keyValues)
        {
            Dictionary<string, object> keys = new Dictionary<string, object>();
            foreach (var item in keyValues)
            {
                if (item.Value.Length == 0)
                {
                    continue;
                }
                if (item.Value.Length == 1 && item.Value[0] == null)
                {
                    continue;
                }

                keys.Add(item.Key, item.Value);


            }
            return keys;
        }

        public static Dictionary<string, object> ConvertNullToStringEmpty(this Dictionary<string, object> keyValues)
        {
            Dictionary<string, object> keys = new Dictionary<string, object>();
            foreach (var item in keyValues)
            {
                if (item.Value == null)
                {
                    keys.Add(item.Key, "");
                }
                else
                {
                    keys.Add(item.Key, item.Value);
                }

            }
            return keys;
        }

        public async static Task<List<Dictionary<string, object>>> MultiDataTableToDictionaryAnsy(this Task<DataTable> dt)
        {
            var tmp = await dt;
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            if (tmp.Rows.Count > 0)
            {
                foreach (DataRow item in tmp.Rows)
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();

                    if (tmp.Columns.Count > 0)
                    {
                        for (int i = 0; i < tmp.Columns.Count; i++)
                        {
                            if (item[tmp.Columns[i].ColumnName].GetType() == typeof(System.DBNull))
                            {
                                data.Add(tmp.Columns[i].ColumnName, null);
                                continue;
                            }
                            else
                            {
                                data.Add(tmp.Columns[i].ColumnName, item[tmp.Columns[i].ColumnName]);
                                continue;
                            }


                        }
                    }
                    list.Add(data);
                }
            }
            return list;
        }

        public static List<Dictionary<string, object>> MultiDataTableToDictionary(this DataTable dt)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow item in dt.Rows)
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();

                    if (dt.Columns.Count > 0)
                    {
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            if (item[dt.Columns[i].ColumnName].GetType() == typeof(System.DBNull))
                            {
                                data.Add(dt.Columns[i].ColumnName, null);
                                continue;
                            }
                            else
                            {
                                data.Add(dt.Columns[i].ColumnName, item[dt.Columns[i].ColumnName]);
                                continue;
                            }


                        }
                    }
                    list.Add(data);
                }
            }
            return list;
        }


        /// <summary>
        /// 这个方法只接受GetModel
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static Dictionary<string, object> SingleDataTableToDictionary(this DataTable dt)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            string[] strColumns = null;
            if (dt.Columns.Count > 0)
            {

                int columnNum = 0;
                columnNum = dt.Columns.Count;
                strColumns = new string[columnNum];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Rows.Count == 0)
                    {
                        data.Add(dt.Columns[i].ColumnName, null);
                        continue;
                    }

                    if (dt.Rows[0][dt.Columns[i].ColumnName].GetType() == typeof(System.DBNull))
                    {
                        data.Add(dt.Columns[i].ColumnName, null);
                        continue;
                    }
                    else
                    {
                        data.Add(dt.Columns[i].ColumnName, dt.Rows[0][dt.Columns[i].ColumnName]);
                        continue;
                    }


                }
            }
            return data;
        }
        ///// <summary>
        ///// 获取某一列的所有值
        ///// </summary>
        ///// <typeparam name="T">列数据类型</typeparam>
        ///// <param name="dtSource">数据表</param>
        ///// <param name="filedName">列名</param>
        ///// <returns></returns>
        //public static List<T> GetColumnValues<T>(DataTable dtSource, string filedName)
        //{
        //    return (from r in dtSource.AsEnumerable() select r.Field<T>("ID")).ToList<T>();
        //}




        public static string[] DataTableGetColumnsNameByDataTable(this DataTable dt)
        {
            string[] strColumns = null;


            if (dt.Columns.Count > 0)
            {
                int columnNum = 0;
                columnNum = dt.Columns.Count;
                strColumns = new string[columnNum];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    strColumns[i] = dt.Columns[i].ColumnName;
                }
            }


            return strColumns;
        }
    }
}
